package com.ml.mall.api.log;


import com.ml.mall.annotation.LogMethodInvokeInformation;
import com.ml.mall.api.BaseController;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.log.OperationLogQueryDto;
import com.ml.mall.bean.res.log.OperationLogQueryRes;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.entity.log.OperationLog;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.service.log.OperationLogService;
import com.ml.mall.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyyang
 * Created 2021-01-07
 */

@RestController
@RequestMapping("/operation_log")
@Api(tags = "操作日志")
public class OperationLogController extends BaseController {

    @Autowired
    private OperationLogService operationLogService;

    @RequestMapping(method = RequestMethod.POST, path = "/query")
    @ApiOperation("查询操作日志")
//  @RequiresPermissions(PermissionConst.OPERATION_LOG_QUERY)
    @LogMethodInvokeInformation("查询操作日志")
    public ApiRestResult operationLogQuery(@RequestBody @Valid OperationLogQueryDto operationLogQueryDto) {
        SysUser currentUser = getCurrentUser();
        if(CommonConsts.ROLE_TYPE_ADMIN != currentUser.getRoleType()){
            operationLogQueryDto.setOperUserId(currentUser.getAcc());
        }
        return ApiRestResult.ok(operationLogService.operationLogQuery(operationLogQueryDto));
    }


    @RequestMapping(method = RequestMethod.POST, path = "/download")
    @ApiOperation("操作日志下载")
//  @RequiresPermissions(PermissionConst.OPERATION_LOG_DOWNLOAD)
    public void operationLogDownload(@RequestBody @Valid OperationLogQueryDto operationLogQueryDto, HttpServletResponse response) {
        SysUser currentUser = getCurrentUser();
        if(CommonConsts.ROLE_TYPE_ADMIN != currentUser.getRoleType()){
            operationLogQueryDto.setOperUserId(currentUser.getAcc());
        }
        List<OperationLogQueryRes> list = operationLogService.operationLogQuery(operationLogQueryDto).getRecords();
        ExcelUtils.exportOperationLogExcel("用户操作日志", list, response);
    }


}
